<!--
 * @Author: Rain
 * @Date: 2023/12/03 10:50:49
 * @LastEditTime: 2023/12/03 11:36:52
 * @Description: Do Something
-->
<template>
  <div class="app-container">
    <!--工具栏-->
    <div class="head-container">
      <div class="w-full flex items-center flex-end justify-end">
        <!-- 搜索 -->
        <label class="el-form-item-label">海报类型</label>
        <el-select v-model="query.posterType" filterable placeholder="请选择">
          <el-option
            v-for="item in dict.poster_type"
            :key="item.id"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
        <label class="el-form-item-label">状态</label>
        <el-select v-model="query.status" filterable placeholder="请选择">
          <el-option label="有效" value="1" />
          <el-option label="无效" value="0" />
        </el-select>
        <rrOperation :crud="crud" />
      </div>
      <!--如果想在工具栏加入更多按钮，可以使用插槽方式， slot = 'left' or 'right'-->
      <crudOperation :permission="permission" />
      <!--表单组件-->
      <el-dialog
        :close-on-click-modal="false"
        :before-close="crud.cancelCU"
        :visible.sync="crud.status.cu > 0"
        :title="crud.status.title"
        width="500px"
      >
        <el-form ref="form" :model="form" :rules="rules" size="small" label-width="80px">
          <el-form-item
            label="海报类型"
            prop="posterType"
            :rules="[{ required: true, message: '请选择海报类型', trigger: 'blur' }]"
          >
            <el-select
              v-model="form.posterType"
              filterable
              placeholder="请选择"
              class="w-full"
            >
              <el-option
                v-for="item in dict.poster_type"
                :key="item.id"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item
            label="状态"
            prop="status"
            :rules="[{ required: true, message: '请选择状态', trigger: 'blur' }]"
          >
            <el-select
              v-model="form.status"
              filterable
              placeholder="请选择"
              class="w-full"
            >
              <el-option
                v-for="item in dict.poster_status"
                :key="item.id"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item
            label="附件上传"
            prop="posterUrl"
            :rules="[{ required: true, message: '请上传海报地址', trigger: 'blur' }]"
          >
            <div class="tips">建议上传500*750像素的图片</div>
            <uploadImg
              ref="uploadImage"
              :show-tip="false"
              :src="form.posterUrl"
              @upload="($event) => (form.posterUrl = $event)"
            />
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="default" @click="crud.cancelCU">取消</el-button>
          <el-button
            :loading="crud.status.cu === 2"
            type="primary"
            @click="submitAction"
          >确认</el-button>
        </div>
      </el-dialog>
      <!--表格渲染-->
      <el-table
        ref="table"
        v-loading="crud.loading"
        :data="crud.data"
        size="small"
        style="width: 100%"
        @selection-change="crud.selectionChangeHandler"
      >
        <el-table-column type="selection" width="55" />
        <el-table-column prop="posterType" label="海报类型">
          <template slot-scope="scope">
            {{ dict.label.poster_type[scope.row.posterType] }}
          </template>
        </el-table-column>
        <el-table-column prop="posterUrl" label="海报地址">
          <template slot-scope="{ row }">
            <img
              :src="row.posterUrl | imgTransform"
              alt=""
              style="width: 80px; height: 80px"
            >
          </template>
        </el-table-column>
        <el-table-column prop="status" label="状态">
          <template slot-scope="scope">
            {{ dict.label.poster_status[scope.row.status] }}
          </template>
        </el-table-column>
        <el-table-column
          v-if="checkPer(['admin', 'poster:edit', 'poster:del'])"
          label="操作"
          width="150px"
          align="center"
        >
          <template slot-scope="scope">
            <udOperation :data="scope.row" :permission="permission" />
          </template>
        </el-table-column>
      </el-table>
      <!--分页组件-->
      <pagination />
    </div>
  </div>
</template>

<script>
import crudPoster from '@/api/poster'
import CRUD, { presenter, header, form, crud } from '@crud/crud'
import rrOperation from '@crud/RR.operation'
import crudOperation from '@crud/CRUD.operation'
import udOperation from '@crud/UD.operation'
import pagination from '@crud/Pagination'
import uploadImg from '@/components/uploadImage'

const defaultForm = {
  id: null,
  posterType: null,
  posterUrl: null,
  createTime: null,
  updateTime: null,
  createby: null,
  updateby: null,
  status: null
}
export default {
  name: 'Poster',
  components: { pagination, crudOperation, rrOperation, udOperation, uploadImg },
  mixins: [presenter(), header(), form(defaultForm), crud()],
  dicts: ['poster_type', 'poster_status'],
  cruds() {
    return CRUD({
      title: '海报',
      url: 'api/poster',
      idField: 'id',
      sort: 'id,desc',
      crudMethod: { ...crudPoster }
    })
  },
  data() {
    return {
      permission: {
        add: ['admin', 'poster:add'],
        edit: ['admin', 'poster:edit'],
        del: ['admin', 'poster:del']
      },
      rules: {},
      queryTypeOptions: [
        { key: 'posterType', display_name: '海报类型' },
        { key: 'status', display_name: '状态' }
      ]
    }
  },
  methods: {
    // 钩子：在获取表格数据之前执行，false 则代表不获取数据
    [CRUD.HOOK.beforeRefresh]() {
      return true
    },
    [CRUD.HOOK.beforeToEdit](crud, form) {
      const { posterType, status } = form
      form.posterType = posterType + ''
      form.status = status + ''
    },
    submitAction() {
      this.$refs['form'].validate((validate) => {
        if (validate) {
          this.$refs['uploadImage'].handleUpload().then((res) => {
            this.crud.submitCU()
          })
        }
      })
    }
  }
}
</script>

<style scoped>
.tips {
  color: #409eff;
  font-size: 12px;
}
</style>
